using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elderlyhealth.DAL;

namespace Elderlyhealth.BLL
{
    public class CustomerService
    {
        DataClasses1DataContext db = new DataClasses1DataContext();

        public int CheckLogin(string name, string password)
        {
            // 通过 MyPetShop.DAL 数据访问层中的 Customer 类查询输入的用户名和密码是否正确，若正确则返回相应的用户对象，否则返回 null
            Trelative子女 trelative = (from r in db.Trelative子女
                                     where r.名字 == name && r.密码 == password
                                     select r).FirstOrDefault();
            if (trelative != null)  // 用户名和密码正确
            {
                return trelative.子女id;
            }
            else  // 用户名或密码错误
            {
                return 0;
            }
        }

        /// <summary>
        /// 修改用户Id对应用户的密码
        /// </summary>
        /// <param name="customerId">用户Id</param>
        /// <param name="password">新密码</param>
        public void ChangePassword(int 子女id, string password)
        {
            Trelative子女 trelative = (from r in db.Trelative子女
                                     where r.子女id == 子女id
                                     select r).First();
            trelative.密码 = password;

            db.SubmitChanges();
        }

        public bool IsNameExist(string name)
        {
            Trelative子女 trelative = (from r in db.Trelative子女
                                     where r.名字 == name
                                     select r).FirstOrDefault();
            return trelative != null;
        }

        public void Insert(string name, string telephone, string password, string email)
        {
            Trelative子女 trelative = new Trelative子女
            {
                名字 = name,
                电话 = telephone,
                密码 = password,
                邮箱 = email
            };
            db.Trelative子女.InsertOnSubmit(trelative);
            db.SubmitChanges();
        }

        public bool IsEmailExist(string name, string email)
        {
            Trelative子女 trelative = (from r in db.Trelative子女
                                     where r.名字 == name && r.邮箱 == email
                                     select r).FirstOrDefault();
            return trelative != null;
        }

        /// <summary>
        /// 将用户密码重置为一个随机的 4 位数字并提交更新
        /// </summary>
        /// <param name="name">输入的用户名</param>
        /// <param name="email">输入的邮箱</param>
        public void ResetPassword(string name, string email)
        {
            // 查询用户
            Trelative子女 trelative = (from r in db.Trelative子女
                                     where r.名字 == name && r.邮箱 == email
                                     select r).FirstOrDefault();

            if (trelative == null)
            {
                throw new Exception("找不到匹配的用户。");
            }

            // 生成 4 位随机数字
            string newPassword = GenerateRandomCode();

            // 更新密码
            trelative.密码 = newPassword;

            // 提交更改到数据库
            db.SubmitChanges();

            // （可选择）在这里可以调用邮件发送逻辑，发送新的密码
        }

        private string GenerateRandomCode()
        {
            Random random = new Random();
            return random.Next(1000, 10000).ToString(); // 生成 1000 到 9999 之间的随机数
        }
    }
}
